﻿<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>3.python bindings</title>
  <link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>

<body class="stackedit">
  <div class="stackedit__html"><h1><a id="hnswlibpython_0"></a>hnswlib的python绑定</h1>
<h2><a id="Python_Bingdings_1"></a>Python Bingdings</h2>
<p>链接: <a href="https://bbs.huaweicloud.com/blogs/281506">link</a></p>
<p>链接: <a href="https://pybind11.readthedocs.io/en/stable/basics.html">pybind11</a></p>
<h3><a id="_8"></a>编组数据类型</h3>
<p>在开始编写 Python 绑定之前，先看看 Python 和 C 如何存储数据以及这会导致哪些类型的问题。首先，让我们定义编组。这个概念由维基百科定义如下：</p>
<pre><code>将对象的内存表示转换为适合存储或传输的数据格式的过程。
</code></pre>
<p><strong>编组是 Python 绑定在准备数据以将其从 Python 移动到 C 或反之亦然时所做的工作</strong>。Python 绑定需要进行编组，因为 Python 和 C 以不同的方式存储数据。C 在内存中以最紧凑的形式存储数据。如果您使用uint8_t，那么它总共将只使用 8 位内存。</p>
<p>另一方面，在 Python 中，一切都是对象。这意味着每个整数在内存中使用几个字节。多少取决于您运行的 Python 版本、操作系统和其他因素。<strong>这意味着您的 Python 绑定将需要为每个跨边界传递的整数将C 整数转换为Python 整数。</strong></p>
<p>其他数据类型在这两种语言之间具有相似的关系。让我们依次来看看：</p>
<p><strong>整数存储计数数字</strong>。Python 以任意精度存储整数，这意味着您可以存储非常非常大的数字。C 指定整数的确切大小。在语言之间移动时需要注意数据大小，以防止 Python 整数值溢出 C 整数变量。</p>
<p><strong>浮点数是带有小数位的数字</strong>。Python 可以存储比 C 大得多（和小得多）的浮点数。这意味着您还必须注意这些值以确保它们保持在范围内。</p>
<p><strong>复数是带有虚部的数字</strong>。虽然 Python 具有内置复数，而 C 具有复数，但没有用于在它们之间编组的内置方法。要封送复数，您需要在 C 代码中构建struct或class来管理它们。</p>
<p><strong>字符串是字符序列</strong>。作为这样一种常见的数据类型，当您创建 Python 绑定时，字符串将被证明是相当棘手的。与其他数据类型一样，Python 和 C 以完全不同的格式存储字符串。（与其他数据类型不同，这也是 C 和 C++ 不同的领域，这增加了乐趣！）您将研究的每个解决方案都有略微不同的处理字符串的方法。</p>
<p><strong>布尔变量只能有两个值</strong>。由于它们在 C 中得到支持，因此将它们编组将被证明是相当简单的。</p>
<p>除了数据类型转换之外，在构建 Python 绑定时还需要考虑其他问题。</p>
<h3><a id="_31"></a>了解可变和不可变值</h3>
<p>除了所有这些数据类型之外，您还必须了解 Python 对象如何可变或不可变。当谈到传值或传引用时，C 有一个类似的函数参数概念。在 C 中，所有参数都是按值传递的。如果要允许函数更改调用方中的变量，则需要传递指向该变量的指针。</p>
<p>您可能想知道是否可以通过使用指针将不可变对象简单地传递给 C 来绕过不可变限制。除非你走到丑陋和不可移植的极端，否则Python 不会给你一个指向 object 的指针，所以这行不通。如果您想用 C 修改 Python 对象，那么您需要采取额外的步骤来实现这一点。</p>
<p>因此，可以将不变性添加到您创建 Python 绑定时要考虑的项目清单中。在创建此清单的宏伟之旅中，最后一站是如何处理 Python 和 C 处理内存管理的不同方式。</p>
<h3><a id="_38"></a>管理内存</h3>
<p>C 和 Python管理内存的方式不同。在 C 中，开发人员必须管理所有内存分配并确保它们被释放一次且仅一次。Python 使用垃圾收集器为您处理这个问题。</p>
<p>虽然这些方法中的每一种都有其优点，但它确实为创建 Python 绑定添加了额外的麻烦。您需要知道每个对象的内存分配在哪里，并确保它只在语言障碍的同一侧被释放。</p>
<p>例如，当您设置x = 3. 用于此的内存在 Python 端分配，需要进行垃圾收集。幸运的是，使用 Python 对象，很难做任何其他事情。看看 C 中的逆向，直接分配一块内存：</p>
<pre><code class="prism language-javascript">int<span class="token operator">*</span> iPtr <span class="token operator">=</span> <span class="token punctuation">(</span>int<span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token function">sizeof</span><span class="token punctuation">(</span>int<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>int* iPtr = (int*)malloc(sizeof(int));<br>
执行此操作时，您需要确保在 C 中释放此指针。这可能意味着手动将代码添加到 Python 绑定中以执行此操作。</p>
<h3><a id="_53"></a>环境配置</h3>
<p>1.安装的C++ 库和命令行调用路径<br>
2.Python开发工具（Python 3.6或更高版本）<br>
*3.虚拟环境<br>
4.invoke工具（本项目使用cmake）<a href="https://docs.pyinvoke.org/en/stable/index.html">invoke</a></p>
<h2><a id="_59"></a>绑定工具</h2>
<h3><a id="ctypes_60"></a>ctypes</h3>
<h3><a id="CFFI_61"></a>CFFI</h3>
<h3><a id="PyBind11_62"></a>PyBind11</h3>
<p>PyBind11使用 C++ 来指定和构建模块，使其能够利用 C++ 中的元编程工具。生成的 Python 绑定PyBind11是一个完整的 Python 模块，可以直接导入和使用。</p>
<p>PyBind11以Boost::Python库为蓝本并具有类似的界面。但是，它将其使用限制为 C++11 和更新版本，与支持所有内容的 Boost 相比，这使其能够简化和加快处理速度。</p>
<h4><a id="_70"></a>安装</h4>
<pre><code class="prism language-javascript">pip install pybind11
</code></pre>
<h4><a id="_75"></a>编写绑定</h4>
<p><strong>调用普通函数</strong></p>
<pre><code class="prism language-javascript"><span class="token comment">// pybind11_wrapper.cpp</span>
#include <span class="token operator">&lt;</span>pybind11<span class="token operator">/</span>pybind11<span class="token punctuation">.</span>h<span class="token operator">&gt;</span>

<span class="token constant">PYBIND11_MODULE</span><span class="token punctuation">(</span> <span class="token parameter">模块名<span class="token punctuation">,</span> 模块实例对象</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
    m<span class="token punctuation">.</span><span class="token function">doc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">"pybind11 example"</span><span class="token punctuation">;</span>   <span class="token comment">//可选，说明这个模块是做什么的</span>
    <span class="token comment">//封装的具体操作。这些操作包括普通的函数的封装，类的访问等</span>
    <span class="token function">def</span><span class="token punctuation">(</span> <span class="token string">"给python调用方法名"</span>， <span class="token operator">&amp;</span>实际操作的函数， <span class="token string">"函数功能说明"</span> <span class="token punctuation">)</span><span class="token punctuation">.</span> <span class="token comment">//其中函数功能说明为可选</span>
<span class="token punctuation">}</span>
<span class="token comment">//在python中使用 模块名.函数名 来访问</span>
</code></pre>
<pre><code class="prism language-javascript">#include <span class="token operator">&lt;</span>pybind11<span class="token operator">/</span>pybind11<span class="token punctuation">.</span>h<span class="token operator">&gt;</span>

int <span class="token function">add</span><span class="token punctuation">(</span> <span class="token parameter">int i<span class="token punctuation">,</span> int j</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> i<span class="token operator">+</span>j<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token constant">PYBIND11_MODULE</span><span class="token punctuation">(</span> <span class="token parameter">py2cpp<span class="token punctuation">,</span> m</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
    m<span class="token punctuation">.</span><span class="token function">doc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">"pybind11 example"</span><span class="token punctuation">;</span>
    m<span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"add"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>add<span class="token punctuation">,</span> <span class="token string">"add two number"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//在python中py2cpp.add进行调用</span>
</code></pre>
<p><strong>调用类函数的成员函数</strong></p>
<p>pybind11提供了访问成员函数的能力。和访问普通的函数不同，访问成员函数之前先要生成一个对象实例，因此，对任意的一个类而言，都需要有两步，第一步是包装一个实例构造方法，另外一个是成员函数的范围方式</p>
<pre><code class="prism language-javascript"><span class="token literal-property property">pybind11</span><span class="token operator">:</span><span class="token operator">:</span>class_<span class="token operator">&lt;</span>命名空间<span class="token operator">:</span><span class="token operator">:</span>类名<span class="token operator">&gt;</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token string">"在python中构造这个类的方法名"</span> <span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>pybind11<span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>            <span class="token comment">//构造器，对应的是c++类的构造函数，如果没有这个构造函数，或者参数对不是会调用失败</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span> <span class="token string">"python中函数名"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>命名空间<span class="token operator">:</span><span class="token operator">:</span>类名<span class="token operator">:</span><span class="token operator">:</span>函数名 <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<pre><code class="prism language-javascript">#include <span class="token operator">&lt;</span>pybind11<span class="token operator">/</span>pybind11<span class="token punctuation">.</span>h<span class="token operator">&gt;</span>

<span class="token keyword">class</span> <span class="token class-name">Hello</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token keyword">void</span> <span class="token function">say</span><span class="token punctuation">(</span> <span class="token parameter"><span class="token keyword">const</span> <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span>string s</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span>cout <span class="token operator">&lt;&lt;</span> s <span class="token operator">&lt;&lt;</span> std<span class="token operator">:</span><span class="token operator">:</span>endl<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token constant">PYBIND11_MODULE</span><span class="token punctuation">(</span> <span class="token parameter">py2cpp<span class="token punctuation">,</span> m</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
    m<span class="token punctuation">.</span><span class="token function">doc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">"pybind11 example"</span><span class="token punctuation">;</span>

    <span class="token literal-property property">pybind11</span><span class="token operator">:</span><span class="token operator">:</span>class_<span class="token operator">&lt;</span>Hello<span class="token operator">&gt;</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token string">"Hello"</span> <span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>pybind11<span class="token operator">:</span><span class="token operator">:</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span> <span class="token string">"say"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Hello<span class="token operator">:</span><span class="token operator">:</span>say <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//python 调用方式</span>
<span class="token comment">//1, 先通过构造器来构建实例，方法为 模块名.构造器名</span>
<span class="token comment">//2，调用对应的方法， 模块名.方法名</span>
<span class="token comment">//例如本例子需要如下调用</span>
<span class="token comment">// c=py2cpp.Hello()</span>
<span class="token comment">// c.say()</span>
</code></pre>
<p>带参的构造函数</p>
<pre><code class="prism language-javascript">#include <span class="token operator">&lt;</span>iostream<span class="token operator">&gt;</span>
#include <span class="token operator">&lt;</span>pybind11<span class="token operator">/</span>pybind11<span class="token punctuation">.</span>h<span class="token operator">&gt;</span>

<span class="token keyword">class</span> <span class="token class-name">Test</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Test</span><span class="token punctuation">(</span> int i<span class="token punctuation">,</span> int j <span class="token punctuation">)</span>
    <span class="token operator">:</span><span class="token function">mI</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">mJ</span><span class="token punctuation">(</span><span class="token parameter">j</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

    <span class="token punctuation">}</span>

    <span class="token keyword">void</span> <span class="token function">Print</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span>cout <span class="token operator">&lt;&lt;</span><span class="token string">"i= "</span> <span class="token operator">&lt;&lt;</span> mI <span class="token operator">&lt;&lt;</span><span class="token string">" j= "</span> <span class="token operator">&lt;&lt;</span>mJ <span class="token operator">&lt;&lt;</span> std<span class="token operator">:</span><span class="token operator">:</span>endl<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    int mI<span class="token punctuation">;</span>
    int mJ<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token constant">PYBIND11_MODULE</span><span class="token punctuation">(</span> <span class="token parameter">py2cpp<span class="token punctuation">,</span> m</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
    m<span class="token punctuation">.</span><span class="token function">doc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">"pybind11 example"</span><span class="token punctuation">;</span>
    <span class="token literal-property property">pybind11</span><span class="token operator">:</span><span class="token operator">:</span>class_<span class="token operator">&lt;</span>Test<span class="token operator">&gt;</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token string">"Test"</span> <span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span> pybind11<span class="token operator">:</span><span class="token operator">:</span>init<span class="token operator">&lt;</span> int <span class="token punctuation">,</span> int <span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>  <span class="token comment">//构造器的模版参数列表中需要按照构造函数的参数类型填入才能调用对应的参数</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span> <span class="token string">"print"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Test<span class="token operator">:</span><span class="token operator">:</span>Print <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//本例子需要如下调用</span>
<span class="token comment">// c=py2cpp.Test(1,2)</span>
<span class="token comment">// c.print()</span>
</code></pre>
<p>参数<br>
arg是可用于将元数据传递到module_::def()</p>
<pre><code class="prism language-javascript">m<span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"add"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>add<span class="token punctuation">,</span> <span class="token string">"A function which adds two numbers"</span><span class="token punctuation">,</span>
      <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"i"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"j"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>访问struct/class的公有非成员</p>
<pre><code class="prism language-javascript"><span class="token function">def_readwrite</span><span class="token punctuation">(</span><span class="token string">"在python中访问的变量名"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>要访问的变量 <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h3><a id="hnswlib_185"></a>hnswlib</h3>
<pre><code class="prism language-javascript"><span class="token constant">PYBIND11_PLUGIN</span><span class="token punctuation">(</span><span class="token parameter">hnswlib</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span>module <span class="token function">m</span><span class="token punctuation">(</span><span class="token string">"hnswlib"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span>class_<span class="token operator">&lt;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token string">"Index"</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>createFromParams<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"params"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
           <span class="token comment">/* WARNING: Index::createFromIndex is not thread-safe with Index::addItems */</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>createFromIndex<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"index"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span>init<span class="token operator">&lt;</span><span class="token keyword">const</span> <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span>string <span class="token operator">&amp;</span><span class="token punctuation">,</span> <span class="token keyword">const</span> int<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"space"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"dim"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"init_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>init_new_index<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"M"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ef_construction"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"random_seed"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"knn_query"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>knnQuery_return_numpy<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"k"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"add_items"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>addItems<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ids"</span><span class="token punctuation">)</span> <span class="token operator">=</span> py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">none</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_items"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token punctuation">,</span> float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getDataReturnList<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ids"</span><span class="token punctuation">)</span> <span class="token operator">=</span> py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">none</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_ids_list"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getIdsList<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"set_ef"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>set_ef<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ef"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"set_num_threads"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>set_num_threads<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"save_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>saveIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"path_to_index"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"load_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>loadIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"path_to_index"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"mark_deleted"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>markDeleted<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"label"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"unmark_deleted"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>unmarkDeleted<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"label"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"resize_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>resizeIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"new_size"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_max_elements"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getMaxElements<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_current_count"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getCurrentCount<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_readonly</span><span class="token punctuation">(</span><span class="token string">"space"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>space_name<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_readonly</span><span class="token punctuation">(</span><span class="token string">"dim"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>dim<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_readwrite</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>num_threads_default<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property</span><span class="token punctuation">(</span><span class="token string">"ef"</span><span class="token punctuation">,</span>
          <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>ef_ <span class="token operator">:</span> index<span class="token punctuation">.</span>default_ef<span class="token punctuation">;</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
          <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter">Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index<span class="token punctuation">,</span> <span class="token keyword">const</span> size_t ef_</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            index<span class="token punctuation">.</span>default_ef<span class="token operator">=</span>ef_<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>index<span class="token punctuation">.</span>appr_alg<span class="token punctuation">)</span>
              index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>ef_ <span class="token operator">=</span> ef_<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>max_elements_ <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"element_count"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>cur_element_count <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"ef_construction"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>ef_construction_ <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"M"</span><span class="token punctuation">,</span>  <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span><span class="token constant">M_</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>

        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">pickle</span><span class="token punctuation">(</span>
            <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>ind</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// __getstate__</span>
                <span class="token keyword">return</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">make_tuple</span><span class="token punctuation">(</span>ind<span class="token punctuation">.</span><span class="token function">getIndexParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* Return dict (wrapped in a tuple) that fully encodes state of the Index object */</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span>tuple t</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// __setstate__</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span>
                    <span class="token keyword">throw</span> <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">runtime_error</span><span class="token punctuation">(</span><span class="token string">"Invalid state!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token keyword">return</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">createFromParams</span><span class="token punctuation">(</span>t<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cast<span class="token operator">&lt;</span>py<span class="token operator">:</span><span class="token operator">:</span>dict<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"__repr__"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token string">"&lt;hnswlib.Index(space='"</span> <span class="token operator">+</span> a<span class="token punctuation">.</span>space_name <span class="token operator">+</span> <span class="token string">"', dim="</span><span class="token operator">+</span>std<span class="token operator">:</span><span class="token operator">:</span><span class="token function">to_string</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>dim<span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">")&gt;"</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span>class_<span class="token operator">&lt;</span>BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token string">"BFIndex"</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span>init<span class="token operator">&lt;</span><span class="token keyword">const</span> <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span>string <span class="token operator">&amp;</span><span class="token punctuation">,</span> <span class="token keyword">const</span> int<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"space"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"dim"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"init_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>init_new_index<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"knn_query"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>knnQuery_return_numpy<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"k"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"add_items"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>addItems<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ids"</span><span class="token punctuation">)</span> <span class="token operator">=</span> py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">none</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"delete_vector"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>deleteVector<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"label"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"save_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>saveIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"path_to_index"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"load_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>loadIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"path_to_index"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"__repr__"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> BFIndex<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token string">"&lt;hnswlib.BFIndex(space='"</span> <span class="token operator">+</span> a<span class="token punctuation">.</span>space_name <span class="token operator">+</span> <span class="token string">"', dim="</span><span class="token operator">+</span>std<span class="token operator">:</span><span class="token operator">:</span><span class="token function">to_string</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>dim<span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">")&gt;"</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> m<span class="token punctuation">.</span><span class="token function">ptr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre>
<p>带参类成员的成员函数</p>
<pre><code class="prism language-javascript"> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span>class_<span class="token operator">&lt;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token string">"Index"</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>createFromParams<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"params"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
           <span class="token comment">/* WARNING: Index::createFromIndex is not thread-safe with Index::addItems */</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>createFromIndex<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"index"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span>init<span class="token operator">&lt;</span><span class="token keyword">const</span> <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span>string <span class="token operator">&amp;</span><span class="token punctuation">,</span> <span class="token keyword">const</span> int<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"space"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"dim"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"init_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>init_new_index<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"M"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ef_construction"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"random_seed"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"knn_query"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>knnQuery_return_numpy<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"k"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"add_items"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>addItems<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ids"</span><span class="token punctuation">)</span> <span class="token operator">=</span> py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">none</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_items"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token punctuation">,</span> float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getDataReturnList<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ids"</span><span class="token punctuation">)</span> <span class="token operator">=</span> py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">none</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_ids_list"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getIdsList<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"set_ef"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>set_ef<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"ef"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"set_num_threads"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>set_num_threads<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"save_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>saveIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"path_to_index"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"load_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>loadIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"path_to_index"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"mark_deleted"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>markDeleted<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"label"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"unmark_deleted"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>unmarkDeleted<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"label"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"resize_index"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>resizeIndex<span class="token punctuation">,</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">arg</span><span class="token punctuation">(</span><span class="token string">"new_size"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_max_elements"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getMaxElements<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"get_current_count"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>getCurrentCount<span class="token punctuation">)</span>
</code></pre>
<p>静态属性</p>
<pre><code class="prism language-javascript"><span class="token punctuation">.</span><span class="token function">def_readonly</span><span class="token punctuation">(</span><span class="token string">"space"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>space_name<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_readonly</span><span class="token punctuation">(</span><span class="token string">"dim"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>dim<span class="token punctuation">)</span>
</code></pre>
<p>访问非公有成员（动态获取新属性）</p>
<pre><code class="prism language-javascript"><span class="token punctuation">.</span><span class="token function">def_readwrite</span><span class="token punctuation">(</span><span class="token string">"num_threads"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span>num_threads_default<span class="token punctuation">)</span>
</code></pre>
<p>访问私有变量<br>
通过def_property的定义，我们就可以像访问python的property风格那样访问name_。</p>
<pre><code class="prism language-javascript"><span class="token punctuation">.</span><span class="token function">def_property</span><span class="token punctuation">(</span><span class="token string">"ef"</span><span class="token punctuation">,</span>
          <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>ef_ <span class="token operator">:</span> index<span class="token punctuation">.</span>default_ef<span class="token punctuation">;</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
          <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter">Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index<span class="token punctuation">,</span> <span class="token keyword">const</span> size_t ef_</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            index<span class="token punctuation">.</span>default_ef<span class="token operator">=</span>ef_<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>index<span class="token punctuation">.</span>appr_alg<span class="token punctuation">)</span>
              index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>ef_ <span class="token operator">=</span> ef_<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"max_elements"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>max_elements_ <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"element_count"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>cur_element_count <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"ef_construction"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span>ef_construction_ <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">def_property_readonly</span><span class="token punctuation">(</span><span class="token string">"M"</span><span class="token punctuation">,</span>  <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">return</span> index<span class="token punctuation">.</span>index_inited <span class="token operator">?</span> index<span class="token punctuation">.</span>appr_alg<span class="token operator">-</span><span class="token operator">&gt;</span><span class="token constant">M_</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p>Lambda绑定</p>
<pre><code class="prism language-javascript"> <span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span><span class="token string">"__repr__"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token string">"&lt;hnswlib.Index(space='"</span> <span class="token operator">+</span> a<span class="token punctuation">.</span>space_name <span class="token operator">+</span> <span class="token string">"', dim="</span><span class="token operator">+</span>std<span class="token operator">:</span><span class="token operator">:</span><span class="token function">to_string</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>dim<span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">")&gt;"</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>Pickling support</p>
<pre><code class="prism language-javascript"><span class="token punctuation">.</span><span class="token function">def</span><span class="token punctuation">(</span>py<span class="token operator">:</span><span class="token operator">:</span><span class="token function">pickle</span><span class="token punctuation">(</span>
            <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">const</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>ind</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// __getstate__</span>
                <span class="token keyword">return</span> <span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">make_tuple</span><span class="token punctuation">(</span>ind<span class="token punctuation">.</span><span class="token function">getIndexParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* Return dict (wrapped in a tuple) that fully encodes state of the Index object */</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">py</span><span class="token operator">:</span><span class="token operator">:</span>tuple t</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// __setstate__</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span>
                    <span class="token keyword">throw</span> <span class="token literal-property property">std</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">runtime_error</span><span class="token punctuation">(</span><span class="token string">"Invalid state!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token keyword">return</span> Index<span class="token operator">&lt;</span>float<span class="token operator">&gt;</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">createFromParams</span><span class="token punctuation">(</span>t<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cast<span class="token operator">&lt;</span>py<span class="token operator">:</span><span class="token operator">:</span>dict<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<p>Python’s pickle module provides a powerful facility to serialize and de-serialize a Python object graph into a binary data stream. To pickle and unpickle C++ classes using pybind11, a py::pickle() definition must be provided.</p>
</div>
</body>

</html>
